<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different URLs to chosen controllers and their actions (functions).
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Config
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/View/Pages/home.ctp)...
 */
	Router::connect('/', array('controller' => 'pages', 'action' =>'home'));	
	
/**
 */
	
	Router::connect('/admin', array('controller' => 'dashboard', 'action' => 'index', 'admin' => true));
	App::import('Model', 'Post');
	$post = new Post();
	$posts = $post->find('all');
	foreach($posts as $p){
		Router::connect('/detail/'.$p['Post']['short_url'], array('controller' => 'detail', 'action' =>'view',$p['Post']['id']));
		Router::connect('/quan-ly-user/tin-dang/chinh-sua/'.$p['Post']['short_url'], array('controller' => 'PostingFree', 'action' =>'edit',$p['Post']['id']));
	}

	//stalls detail
	App::import('Model', array('Shop','Category','Product','SubMenu','RootCategory'));
	$shop = new Shop();
	$category = new Category();
	$product = new Product();
	$footer = new SubMenu();

	$shops = $shop->find('all',array(
					'fields'=>'short_url'));
	$categories = $category->find('all',array(
					'fields'=>array('category_id','short_link')));
					
	$product = $product->find('all',array(
					'fields'=>array('short_url','product_id')));

	foreach ($shops as $s) {
		Router::connect('/gian-hang/'.$s['Shop']['short_url'],array('controller'=>'Stalls','action'=>'view',$s['Shop']['id'],$s['Shop']['short_url']));
		Router::connect('/gian-hang/'.$s['Shop']['short_url'].'/san-pham-moi-nhat',array('controller'=>'Stalls','action'=>'showAllLastest',$s['Shop']['id'],$s['Shop']['short_url']));
		Router::connect('/gian-hang/'.$s['Shop']['short_url'].'/san-pham-ban-chay',array('controller'=>'Stalls','action'=>'showAllBestProduct',$s['Shop']['id'],$s['Shop']['short_url']));

		foreach ($categories as $item) {
			Router::connect('/gian-hang/'.$s['Shop']['short_url'].'/'.$item['Category']['short_link'].'/san-pham-dang-cung-cap',array('controller'=>'Stalls','action'=>'showProductSelling',$s['Shop']['id'],$s['Shop']['short_url'],$item['Category']['short_link']));
			Router::connect('/gian-hang/'.$s['Shop']['short_url'].'/'.$item['Category']['short_link'].'/san-pham-trong-kho',array('controller'=>'Stalls','action'=>'showProductStock',$s['Shop']['id'],$s['Shop']['short_url'],$item['Category']['short_link']));
			
		}
		foreach ($product as $p) {
			Router::connect('/san-pham-'.$p['Product']['short_url'],array('controller'=>'Stalls','action'=>'viewProduct',$p['Product']['product_id'],$s['Shop']['short_url']));
				
			}
	}
	
	//user - gian hang
	Router::connect('/quan-ly-user/gian-hang/tao-gian-hang', array('controller' => 'StallsAdd', 'action' => 'index'));
	Router::connect('/quan-ly-user/gian-hang/chinh-sua', array('controller' => 'StallsAdd', 'action' => 'edit'));
	Router::connect('/quan-ly-user/gian-hang/san-pham', array('controller' => 'StallsPosting', 'action' => 'listProduct'));
	Router::connect('/quan-ly-user/san-pham/them-san-pham',array('controller'=>'StallsPosting','action'=>'index'));
	foreach ($product as $p) {
		Router::connect('/quan-ly-user/san-pham/chinh-sua/'.$p['Product']['short_url'],array('controller'=>'StallsPosting','action'=>'edit',$p['Product']['product_id']));
		Router::connect('/quan-ly-user/san-pham/xoa/'.$p['Product']['short_url'],array('controller'=>'StallsPosting','action'=>'delete',$p['Product']['product_id']));
	}
	
	

   //shop detail 
   $categories = $category->find('all',array(
					'fields'=>array('category_id','short_link')));
  	foreach ($categories as $item) {
  		Router::connect('/shopDetail/'.$item['Category']['short_link'], array('controller' => 'ShopDetail', 'action' =>'index', $item['Category']['category_id']));
	}

	Router::connect('/quan-ly-user', array('controller' => 'Member', 'action' => 'manage'));
	//forgot pw user
	Router::connect('/quen-mat-khau', array('controller' => 'Member', 'action' => 'forgot'));
	Router::connect('/xac-nhan/:code', array('controller' => 'Member', 'action' => 'confirm'),array('pass'=>array('code')));
	Router::connect('/doi-mat-khau/thanh-cong', array('controller' => 'Member', 'action' => 'success'),array('pass'=>array('code')));
	//cap nhat thong tin user
	Router::connect('/quan-ly-user/cap-nhat-thong-tin', array('controller' => 'Member', 'action' => 'updateUser'));
	//doi mat khau user
	Router::connect('/quan-ly-user/doi-mat-khau', array('controller' => 'Member', 'action' => 'changePass'));
	//quan ly tin dang cua user
	Router::connect('/quan-ly-user/tin-dang', array('controller' => 'Member', 'action' => 'managePost'));
	

	//footer
	$info = $footer->find('all',array(
					'fields'=>array('id','short_link')));
  	foreach ($info as $item) {
  		Router::connect('/thong-tin/'.$item['SubMenu']['short_link'], array('controller' => 'Info', 'action' =>'index', $item['SubMenu']['id']));
	}
	
	//root catagory / category
	$category = new Category();
	$rootCat = $category->findAllForRoutes();
	foreach ($rootCat as $item) {
  		Router::connect('/'.$item['root_categories']['short_link'].'/'.$item['categories']['short_link'], 
  		array('controller' => 'Search', 'action' =>'index', $item['root_categories']['id'], $item['categories']['category_id']));
	}

		//
		//Router::connect('/gian-hang/:id/:category/san-pham-dang-cung-cap',array('controller'=>'Stalls','action'=>'showProductSelling'),array('pass'=>array('id','category')));
/**
 * Load all plugin routes. See the CakePlugin documentation on
 * how to customize the loading of plugin routes.
 */
	CakePlugin::routes();

/**
 * Load the CakePHP default routes. Only remove this if you do not want to use
 * the built-in default routes.
 */
	require CAKE . 'Config' . DS . 'routes.php';
